package com.hzw.service.impl;

import com.hzw.bean.*;
import com.hzw.dao.*;
import com.hzw.dto.ArticleCommentDTO;
import com.hzw.service.ArticleCommentService;
import com.hzw.utils.ObjectUtils;
import com.hzw.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/4/1.
 */
@Service
public class ArticleCommentServiceImpl implements ArticleCommentService {
    @Resource
    private ArticleCommentMapper articleCommentMapper;
    @Resource
    private ArticleDetailMapper articleDetailMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private MessageMapper messageMapper;

    /**
     * 新增评论
     * @param articleComment
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addComment(ArticleComment articleComment) {
        //插入文章评论
        String id = UUIDUtils.getUUID();
        articleComment.setId(id);
        articleCommentMapper.insertSelective(articleComment);

        //增加对应文章评论数
        String articleId = articleComment.getArticleId();
        articleDetailMapper.updateCommentNumByArticleId(articleId,1);

        //增加用户的活跃度 +2
        String userId = userMapper.getUserIdByArticleId(articleId);
        userMapper.addActivityLevel(userId,2);

        //判断活跃度+1后是否达到100，若达到则修改会员标志位
        User user = userMapper.selectUserById(userId);
        if(user.getActivityLevel() >= 100 && user.getMemberFlag() == 0){
            user.setMemberFlag(1);
            userMapper.updateByPrimaryKeySelective(user);
        }

        //发消息通知给 被评论用户(作者)
        Message message = new Message();
        String msgId = UUIDUtils.getUUID();
        Article article = articleMapper.selectArticleById(articleId);//通过articleId获取文章标题和作者Id
        String title = article.getTitle();
        String authorId = article.getUserId();
        String userName = userMapper.selectUserNameById(userId);//根据userId获取点赞人用户名
        message.setId(msgId);
        message.setUserId(authorId);
        message.setType(1);
        message.setText("用户“" +userName +"”评论了你的《" + title + "》：" + articleComment.getText());
        messageMapper.insertSelective(message);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteCommentByCommentId(String commentId) {
        //减少文章的评论数
        Article article = articleMapper.selectArticleByCommentId(commentId);
        String articleId = null;
        if (article != null){
            articleId = article.getId();
        }
        //删除评论
        articleCommentMapper.deleteCommentByCommentId(commentId);
        return articleDetailMapper.updateCommentNumByArticleId(articleId,-1);
    }

    @Override
    public List<ArticleCommentDTO> selectCommentByArticleId(String articleId) {
        List<ArticleCommentDTO> articleCommentDTOList = new ArrayList<ArticleCommentDTO>();//评论树列表
        List<ArticleComment> articleCommentList = articleCommentMapper.selectCommentByArticleId(articleId);//评论列表
        if(!ObjectUtils.isNullOrEmpty(articleCommentList) && articleCommentList.size() > 0){
            for (ArticleComment articleComment : articleCommentList) { //将评论列表放入评论树列表中
                User user = userMapper.selectUserById(articleComment.getUserId());
                String headPortraitPath = null;
                String userName = null;
                if(user != null){
                    headPortraitPath = user.getHeadPortraitPath();
                    userName = user.getUserName();
                }
                ArticleCommentDTO articleCommentDTO = new ArticleCommentDTO();
                articleCommentDTO.setArticleComment(articleComment);
                articleCommentDTO.setHeadPortraitPath(headPortraitPath);
                articleCommentDTO.setUserName(userName);
                articleCommentDTOList.add(articleCommentDTO);
            }
        }
        List<ArticleCommentDTO> articleCommentDTOList1 = toRecursionList(null,articleCommentDTOList);
        return articleCommentDTOList1;
    }

    //递归获取文章评论树
    public List<ArticleCommentDTO> toRecursionList(ArticleCommentDTO dto, List<ArticleCommentDTO> commentDtoList){
        if (ObjectUtils.isNullOrEmpty(commentDtoList)){
            return null;
        }
        if (dto==null){
            //先拿主评论
            List<ArticleCommentDTO> fartherList=new ArrayList<>();
            for (ArticleCommentDTO articleCommentDTO : commentDtoList) {
                if (articleCommentDTO.getArticleComment().getType() == 0){
                    fartherList.add(articleCommentDTO);
                }
            }
            //递归查询子评论集合
            for (ArticleCommentDTO fartherDto : fartherList) {
                List<ArticleCommentDTO> sonList = toRecursionList(fartherDto, commentDtoList);
                fartherDto.setSonCommentList(sonList);
            }
            return fartherList;
        }else {
            //拿子评论集合
            List<ArticleCommentDTO> sonList=new ArrayList<>();
            for (ArticleCommentDTO articleCommentDTO : commentDtoList) {
                if (dto.getArticleComment().getId().equals(articleCommentDTO.getArticleComment().getLastCommentId())){
                    sonList.add(articleCommentDTO);
                }
            }
            //设置子评论集合
            //dto.setSonCommentList(sonList);
            //递归查询并设置子评论集合
            for (ArticleCommentDTO sonDto : sonList) {
                List<ArticleCommentDTO> nextSonList = toRecursionList(sonDto, commentDtoList);
                sonDto.setSonCommentList(nextSonList);
            }
            return sonList;
        }
    }
}
